package com.maxima.maximaAttendance.actions;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.maximaAttendance.daos.TimeLogDAO;
import com.maxima.maximaAttendance.daos.UserDAO;
import com.maxima.maximaAttendance.pojos.TimeLog;
import com.maxima.maximaAttendance.pojos.User;
import com.maxima.maximaAttendance.utils.DateOperations;
import com.opensymphony.xwork2.ActionSupport;

public class AdminOperations extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 1L;
	private Map<String, Object> map;
	private Integer id;
	private Integer selectedDate;
	
	public AdminOperations() {
		selectedDate= DateOperations.getTodaysDay();	
	}
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		this.map=map;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	@Override
	public String execute() throws Exception {
		
		String str=  ServletActionContext.getRequest().getParameter("selectedDate");
		
		if(str!=null)
			selectedDate=Integer.parseInt(str);
		else
		{
			selectedDate= DateOperations.getTodaysDay();
		}
		SimpleDateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");
		
		Date date= new Date();
		Calendar cal= Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, selectedDate);
		
		date=cal.getTime();
		
		List<User> lst=new UserDAO().getAllUsers();
		
		List<User> lst1=new TimeLogDAO().getUsersWorkingFromHomeByDate(date);
				
		for (User user : lst1) {
			for (User u : lst) {
				if(u.getId()==user.getId()){
//					u.setWorkFromHome(true);
				}
			}
		}

		int count=0;
		User[][] allUsersInArray= new User[(lst.size()/3)+1][3];
		for (int i = 0; i < allUsersInArray.length; i++) {
			if(lst.size()>count) allUsersInArray[i][0]=lst.get(count++);else break;
			if(lst.size()>count) allUsersInArray[i][1]=lst.get(count++);else break;
			if(lst.size()>count) allUsersInArray[i][2]=lst.get(count++);else break;
		}
		
		map.put("allUsersList", lst);
		map.put("allUsersInArray", allUsersInArray);
		map.put("selectedDate", selectedDate);
		this.setSession(map);
		return SUCCESS;
	}
	public String addWorkFromHome() {
		
		Object s= ServletActionContext.getRequest().getSession().getAttribute("selectedDate");
		if(s!=null)
			selectedDate=(Integer)s;
		else
		{
			selectedDate= DateOperations.getTodaysDay();
		}
		
		Date date= new Date();
		Calendar cal= Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, selectedDate);
		date=cal.getTime();
		
		String[] values= ServletActionContext.getRequest().getParameterValues("checkboxes");
		
		UserDAO userDAO= new UserDAO();
		
		TimeLogDAO timeLogDAO= new TimeLogDAO();
		SimpleDateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");
		
		timeLogDAO.setAllUsersWorkFromHomeByDate(date, false);
		
		if(values!=null)
		for (String str : values) {
			//System.out.println(str);
		//	Date date= new Date();			
			User u=userDAO.getUser(Integer.parseInt(str));
			int x=(timeLogDAO.checkIfPresent(u,date));
			if(x!=-1)
			{
				timeLogDAO.setWorkFromHome(x, true);
			}
			else
			{
				TimeLog t = new TimeLog(date, u);
				t.setWorkFromHome(true);
				t.setComment("Work from home");
				timeLogDAO.addNewTimeLog(t);
			}
		}
		return SUCCESS;
		
	}
	public String selectUser() throws Exception {
		map.put("selectedUser", new UserDAO().getUser(this.getId()));
		this.setSession(map);
		return SUCCESS;
	}
	public String updateUser() throws Exception {
		UserDAO userDAO= new UserDAO();
		
		int x=((User)this.getMap().get("selectedUser")).getId();
		
		User user=userDAO.getUser(x);
//		user.setFirstname(ServletActionContext.getRequest().getParameter("firstname"));
//		user.setLastname(ServletActionContext.getRequest().getParameter("lastname"));
//		user.setEmp_id(Integer.parseInt(ServletActionContext.getRequest().getParameter("emp_id")));
		user.setPassword(ServletActionContext.getRequest().getParameter("password"));
//		user.setProject(ServletActionContext.getRequest().getParameter("project"));
		userDAO.update(x, user);
		map.put("selectedUser", null);
		this.setSession(map);
		return SUCCESS;
	}

	public Integer getSelectedDate() {
		return selectedDate;
	}

	public void setSelectedDate(Integer selectedDate) {
		this.selectedDate = selectedDate;
	}
}
